import { NStatistic, type StatisticProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type StatisticFactoryConstructorType = {
  props?: StatisticProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  suffixSlot?: BaseComponentType;
  prefixSlot?: BaseComponentType;
  labelSlot?: BaseComponentType;
};

export class StatisticFactory {
  private props: StatisticProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot?: BaseComponentType = null;
  private suffixSlot?: BaseComponentType = null;
  private prefixSlot?: BaseComponentType = null;
  private labelSlot?: BaseComponentType = null;

  constructor(param?: StatisticFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.suffixSlot) this.setSuffix(param.suffixSlot);
    if (param?.prefixSlot) this.setPrefix(param.prefixSlot);
    if (param?.labelSlot) this.setLabel(param.labelSlot);
    if (param?.attrs) this.setAttrs(param.attrs);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: StatisticProps) {
    this.props = props;
    return this;
  }
  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }
  setSuffix(suffixSlot: BaseComponentType) {
    this.suffixSlot = suffixSlot;
    return this;
  }
  setPrefix(prefixSlot: BaseComponentType) {
    this.prefixSlot = prefixSlot;
    return this;
  }
  setLabel(labelSlot: BaseComponentType) {
    this.labelSlot = labelSlot;
    return this;
  }

  create() {
    return (
      <NStatistic {...this.props} {...this.attrs}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot as BaseComponentType)
              : null,

          prefix:
            this.prefixSlot != null
              ? () => getSlotsDom(this.prefixSlot as BaseComponentType)
              : null,

          suffix:
            this.suffixSlot != null
              ? () => getSlotsDom(this.suffixSlot as BaseComponentType)
              : null,
          label:
            this.labelSlot != null
              ? () => getSlotsDom(this.labelSlot as BaseComponentType)
              : null,
        }}
      </NStatistic>
    );
  }
}
